package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 购物车业务实现类
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.service.impl
 * @Author: zhouzhijie
 * @CreateTime: 2023-11-22  09:24
 * @Version: 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @description: 添加购物车
     * @author: zhouzhijie
     * @date: 2023/11/22 9:35
     * @param: [shoppingCartDTO]
     * @return:
     **/
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //判断当前购物车的商品是否存在
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        //如果已经存在，只需要将数量加一
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            //查询出来的只有一条数据
            ShoppingCart cart = shoppingCarts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            //根据id来修改商品数量
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果不存在，需要插入一条购物车数据

            //判断当前商品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //本次添加到购物车的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //第一次添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入购物车数据
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * @description: 查看购物车
     * @author: zhouzhijie
     * @date: 2023/11/22 15:54
     * @param: []
     * @return:
     **/
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }

    /**
     * @description: 清空购物车
     * @author: zhouzhijie
     * @date: 2023/11/22 16:05
     * @param: []
     * @return:
     **/
    @Override
    public void cleanShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * @description: 删除购物车商品
     * @author: zhouzhijie
     * @date: 2023/11/22 16:50
     * @param: [shoppingCartDTO]
     * @return:
     **/
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询当前的购物车商品的数量
        //多条件动态查询，只有两种情况（1不存在，2只有一条数据）
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        if (shoppingCarts == null || shoppingCarts.size() == 0) {
            //当前商品查询不存在
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        //查询出来的只有一条数据
        ShoppingCart cart = shoppingCarts.get(0);
        //如果当前商品数量大于1就减1
        if (cart.getNumber() > 1) {
            cart.setNumber(cart.getNumber() - 1);
            //根据id来修改商品数量
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果当前商品数量等于1就删除
            shoppingCartMapper.delete(shoppingCart);
        }
    }
}
